package org.nvframe.util;

import java.util.Random;

import org.newdawn.slick.geom.Vector2f;
import org.nvframe.component.state.Position;
import org.nvframe.entity.Entity;

/**
 * 
 * @author Nik Van Looy
 */
public class NVMathUtils {

	public static final int PIXELS_PER_METER = 30;
	public static final float INFINITE_MASS = Float.MAX_VALUE;
	
	public static float getRandomNumberBetween(float min, float max) {
		Random generator = new Random();
		return (generator.nextFloat() * (max - min)) + min;
	}
	
	public static int getRandomNumberBetween(int min, int max) {
		Random generator = new Random();
		return generator.nextInt(max - min) + min;
	}

	public static float calculateDirectionRad(Entity source, Entity target) {
		Position sourcePos = (Position) source.getRoot().getComponent(Position.class);
		Position targetPos = (Position) target.getComponent(Position.class);
		
		if(sourcePos == null)
			return 0;

		return calculateDirectionRad(sourcePos.getXY(), targetPos.getXY());
	}
	
	public static float calculateDirectionRad(Vector2f source, Vector2f target) {
		double radDirection = Math.atan2(target.y - source.y, target.x - source.x);
		return (float) radDirection;
	}

	public static float angleDifferenceDEG(float currentDirection, float destDirection) {
		// first make the angles positive
		if(currentDirection < 0)
			currentDirection = 360 + currentDirection;
		if(destDirection < 0)
			destDirection = 360 + destDirection;
		
		if(currentDirection < destDirection)
			return destDirection - currentDirection;
		else
			return currentDirection - destDirection;
	}
}
